'use strict';

angular.module('app')
    .controller('creativeDetailController', ['$rootScope', '$scope', '$http', '$state',
        function ($rootScope, $scope, $http, $state) {
            var title = '创意详情';
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            //详情初始化
            var id = $state.params.id;
            activate(id);

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/creative/detail',
                    data: {'id': id}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            //创意审核
            $scope.approve = function (id, state) {
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                    if (state == 3) {
                        $.ajax({
                            url: $scope.app.project_name + '/creative/approve',
                            data: {'id': id, 'state': state}
                        }).then(function (result) {
                            $scope.loading = false;
                            if (result.httpCode == 200) {
                                var msg = result.errorMsg;
                                if (msg != '' && msg != null) {
                                    $scope.alertModal_slow({"content": msg});
                                } else {
                                    $scope.alertModal({"content": "操作成功"});
                                    $state.go('main.zzy.creative.list');
                                }
                            }
                        });
                    } else {
                        $scope.param = {};
                        $scope.param.title = "创意详情";
                        $("#title").text($scope.param.title);
                        $scope.param.id = id;
                        $('#refuseDiv').modal('show', true);
                    }

                })
            }
            // 审核失败保存
            $scope.saveApproveReason = function () {
                var reason = $scope.param.approveReason;
                if (reason == '' || reason == null) {
                    $scope.alertModal({"content": "请输入拒绝原因"});
                    $('#refuseDiv').modal('hide');
                    return;
                }

                $.ajax({
                    url: $scope.app.project_name + '/creative/approve',
                    data: {'id': id, 'state': 4, 'reason': reason}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        // //详情初始化
                        activate(id);
                        //计划下广告初始化
                        $scope.alertModal({"content": "操作成功"});
                    } else {
                        $scope.alertModal({"content": "操作失败"});
                    }
                });
            }
            //预览
            $scope.onPreview = function (data) {
                $scope.getFileExt(data.creativeResourceUrl).indexOf();
                $scope._isShow = true;
                $scope.dataUrl = data.creativeResourceUrl;

            };
            $scope.scale=function (o){
                var zoom=parseInt(o.style.zoom, 10)||100;
                zoom+=event.wheelDelta/6;
                if (zoom>0) o.style.zoom=zoom+'%';
                return false;
            };
            $scope.stopShow = function () {
                $scope._isShow = false;
            };
            $scope.getFileExt=function (str)
            {
                var d=/\.[^\.]+$/.exec(str);
                return d[0].toLowerCase();
            }
        }
    ]);